<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //for ... of ....  ES6语法


        // let arr = ["张三", "李四", "王五"];
        // for (let val of arr) {
        //     console.log(val); // 张三 李四 王五
        // }


        // let obj = {
        //     name: "张三",
        //     age: 20
        // }
        // console.log(obj);
        // for (let val of obj) {
        //     console.log(val); // Uncaught TypeError: obj is not iterable
        // }

        // let set = new Set([1, 2, 3]);
        // for (let val of set) {
        //     console.log(val);// 1 2 3
        // }


        // let map = new Map([
        //     ["name", "张三"],
        //     ["age", 20]
        // ]);
        // for (let val of map) {
        //     console.log(val);// ['name', '张三']  ['age', 20]
        // }


        //
        let arr = ["张三", "李四", "王五"];
        //获取迭代器
        // console.log(arr[Symbol.iterator]()); // Array Iterator {}
        // let iter = arr[Symbol.iterator]();
        // console.log(iter.next()); // {value: '张三', done: false}
        // console.log(iter.next()); // {value: '李四', done: false}
        // console.log(iter.next()); // {value: '王五', done: false}
        // console.log(iter.next()); // {value: undefined, done: true}


        //手写 iterator
        function myiterator(arr) {
            let num = 0;
            return {
                next() {
                    return {
                        value: arr[num++],
                        done: num > arr.length ? true : false
                    }
                }
            }
        }
        let res = myiterator(arr);
        console.log(res.next());
        console.log(res.next());
        console.log(res.next());
        console.log(res.next());
    </script>
</body>

</html>